package com.iflytek.autoCompareTool.common;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;

/**
 * @author llchen12
 * @date 2018/6/8
 */
public class DBManager {

    private QueryRunner aiuiManagerQr;
   // private QueryRunner kcloudToolQr;//没有用到

    private DBManager() {
        init();
    }

    private static DBManager instance;

    public static DBManager getInstance() {
        if (instance == null) {
            synchronized (DBManager.class) {
                if (instance == null) {
                    instance = new DBManager();
                }
            }
        }
        return instance;
    }



    private void init() {
        aiuiManagerQr = new QueryRunner(getAiuiManagerDataSource());
        //kcloudToolQr = new QueryRunner(getKcloudToolDataSource());
    }

    public QueryRunner getAiuiManagerQr() {
        return aiuiManagerQr;
    }

//    public QueryRunner getKcloudToolQr() {
//        return kcloudToolQr;
//    }

    private DataSource getAiuiManagerDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(Config.DB_AIUI_MANAGER_URL);
        dataSource.setPassword(Config.DB_AIUI_MANAGER_PAASORD);
        dataSource.setDriverClassName(Config.DB_AIUI_MANAGER_DRIVER);
        dataSource.setUsername(Config.DB_AIUI_MANAGER_USERBANE);
        dataSource.setInitialSize(1);
        dataSource.setMaxActive(10);
        dataSource.setMinIdle(1);
        dataSource.setMaxWait(60000);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        dataSource.setTestWhileIdle(true);
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        dataSource.setMinEvictableIdleTimeMillis(25200000);
        dataSource.setRemoveAbandoned(true);
        dataSource.setRemoveAbandonedTimeout(1800);
        dataSource.setLogAbandoned(true);

        return dataSource;
    }


    private DataSource getKcloudToolDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(Config.DB_KCLOUD_TOOL_URL);
        dataSource.setPassword(Config.DB_KCLOUD_TOOL_PAASORD);
        dataSource.setDriverClassName(Config.DB_KCLOUD_TOOL_DRIVER);
        dataSource.setUsername(Config.DB_KCLOUD_TOOL_USERBANE);
        dataSource.setInitialSize(1);
        dataSource.setMaxActive(5);
        dataSource.setMinIdle(1);
        dataSource.setMaxWait(60000);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        dataSource.setTestWhileIdle(true);
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        dataSource.setMinEvictableIdleTimeMillis(25200000);
        dataSource.setRemoveAbandoned(true);
        dataSource.setRemoveAbandonedTimeout(1800);
        dataSource.setLogAbandoned(true);

        return dataSource;
    }
}
